package test1.realm;


import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.authz.permission.WildcardPermission;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import test1.permission.BitPermission;

public class MyRealm extends AuthorizingRealm{

	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection princpals){
		SimpleAuthorizationInfo simpleAuthorizationInfo= new SimpleAuthorizationInfo();
		simpleAuthorizationInfo.addRole("role1");
		simpleAuthorizationInfo.addRole("role2");
		simpleAuthorizationInfo.addObjectPermission(new BitPermission(",user1,10"));
		simpleAuthorizationInfo.addObjectPermission(new WildcardPermission("user1:*"));
		simpleAuthorizationInfo.addObjectPermission(new BitPermission(",user2,10"));
		simpleAuthorizationInfo.addObjectPermission(new WildcardPermission("user2:*"));
		return simpleAuthorizationInfo;
	}
	
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){
		
		String username= (String)token.getPrincipal();
		String password= new String((char[])token.getCredentials());
		if(!"zhang".equals(username)){			
			throw new UnknownAccountException();
		}
		if(!"123".equals(password)){
			throw new IncorrectCredentialsException();
		}
		return new SimpleAuthenticationInfo(username,password,getName());
	}
}
